package cn.bjc.org.controller;

import cn.bjc.basic.exception.BusinessException;
import cn.bjc.basic.util.ExcelUtils;
import cn.bjc.basic.util.JsonResult;
import cn.bjc.basic.util.PageList;
import cn.bjc.org.domain.Shop;
import cn.bjc.org.domain.ShopLog;
import cn.bjc.org.dto.ShopDto;
import cn.bjc.org.query.ShopQuery;
import cn.bjc.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

//@Controller
//@ResponseBody
@RestController //controller层,返回json数据
@RequestMapping("/shop")
//接口文档的注解
@Api(value = "店铺的API",description="店铺相关的CRUD功能")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     *  查询所有:
     *  动词:get
     * @return
     */
    //@RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping()//上面的结合
    @ApiOperation(value = "查询所有店铺信息")
    public List<Shop> findAll() {
        return shopService.loadAll();
    }


    /**
     * 查询一个:接收参数id
     * url:http://localhost/shop/id/
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询")
    public Shop findOne(@PathVariable("id") long id) {
        return shopService.loadById(id);
    }

    /**
     * 删除操作
     *  url:http://localhost/shop/id/
     * @param id
     * @return
     * JsonResult:链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            shopService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }

    /**
     * 添加或修改操作
     * 
     * @param Employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加操作接口")
    public JsonResult updateOrAdd(@RequestBody Shop Shop){
        try {
            if(Shop.getId()==null){
                //添加操作
                shopService.save(Shop);
            } else {
                shopService.update(Shop);
            }
            return JsonResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * 
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        try {
            return shopService.pageQuery(query);
        } catch (Exception e){
            e.printStackTrace();
            return new PageList<Shop>();
        }
    }


    /**
     * 批量删除操作
     * @param
     * @return
     * JsonResult:链式编程
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除")
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            shopService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }
    
    //店铺入驻接口
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return JsonResult.me();
        } catch (BusinessException e) { //业务问题
            e.printStackTrace();
            //代码问题(其他不可抗拒因素)
            return JsonResult.me().setMsg(e.getMessage());  //获取的是自定义信息
        } catch (Exception e) { //
            e.printStackTrace();
            //代码问题(其他不可抗拒因素)
            return JsonResult.me().setMsg("系统异常,稍后重试");
        }
    }


    /**
     * 审核通过
     * @param
     * @return
     * JsonResult:链式编程
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "审核通过")
    public JsonResult pass(@RequestBody ShopLog log) {
        try {
            shopService.pass(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败");
        }
    }

    /**
     * 审核驳回
     * @param
     * @return
     * JsonResult:链式编程
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "审核驳回")
    public JsonResult reject(@RequestBody ShopLog log) {
        try {
            shopService.reject(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("驳回失败");
        }
    }
    
    //导出
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        try {
            //1.查询所有shop数据
            List<Shop> list = shopService.loadAll();
            //2.调用工具类 //(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,HttpServletResponse response)
            ExcelUtils.exportExcel(list, null, "店铺数据", Shop.class, "aa.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    //导入
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart(value = "file") MultipartFile file){
        try {
            //导入,调用工具类  MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
            List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);
            //此处先不保存,打印到控制台
            //保存到数据库表中
            list.forEach(System.out::println);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * domain pojo:实体类-->跟数据库中的表对应
     * query:做查询参数使用
     * dto:接收参数或者传参使用
     * @return
     */
    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return shopService.getEcharts();
    }
}
